package cn.hejiang.user.service.impl;

import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.service.impl.BaseServiceImpl;
import cn.hejiang.basic.util.MD5Utils;
import cn.hejiang.basic.util.StrUtils;
import cn.hejiang.user.constant.UserConstants;
import cn.hejiang.user.domain.LoginInfo;
import cn.hejiang.user.domain.User;
import cn.hejiang.user.dto.UserDto;
import cn.hejiang.user.mapper.LoginInfoMapper;
import cn.hejiang.user.mapper.UserMapper;
import cn.hejiang.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

// 继承基础CRUD，只写扩展功能
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    // 注册
    @Override
    public void register(UserDto userDto) {
        // 空校验
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPwd())) {
            throw new BusinessException("请输入完整的信息");
        }

        // 检验两次密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())) {
            throw new BusinessException("输入的两次密码不一致，滚去重输！");
        }

        // 校验手机号是否已注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp != null) {
            throw new BusinessException("该号码已注册，请登录或找回密码");
        }

        // 校验验证码是否已经过期
        Object codeTmp = redisTemplate.opsForValue().get(UserConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null) {
            throw new BusinessException("验证码已过期，请重新获取");
        }

        // 校验验证码是否正确
        String code = codeTmp.toString().split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码输入错误，请重新输入");
        }

        // 保存数据
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);
        // 保存loginInfo
        loginInfoMapper.save(loginInfo);
        // 保存User
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);

        }
    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        // 属性通拷贝
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }

    private User userDto2User(UserDto userDto){
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用盐值加密后的密码
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        System.out.println(md5Pwd);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;


    }
}
